package jumpingnotes.red5;

import java.util.Map;
import java.util.UUID;

import javax.servlet.http.Cookie;

import jumpingnotes.model.ErrorType;
import jumpingnotes.model.Result;
import jumpingnotes.security.Session;
import jumpingnotes.security.SessionManager;
import jumpingnotes.service.remoting.FlashRemotingService;

import org.red5.server.adapter.MultiThreadedApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.net.remoting.RemotingConnection;

public class Application extends MultiThreadedApplicationAdapter
{
	private FlashRemotingService service;
	private static final String SESSION_ID = "SESSION_ID";
	private static final String separator = ",";

	public void setService(FlashRemotingService service)
	{
		this.service = service;
	}
	
	/*private boolean checkPermission(IConnection conn)
	{
		Session session = null;
		Cookie[] cookies = ((RemotingConnection) conn).getHttpRequest().getCookies();
		for(int i=0;i<cookies.length;i++)
		{
			Cookie cookie = cookies[i];
			if(SESSION_ID.equals(cookie.getName()))
			{
				String sessionId = cookie.getValue();

				SessionManager sessionManager = service.getSessionManager();
				session = sessionManager.accessSession(sessionId);

				if (session != null)
				{
					break;
				}
			}
		}
		
		if(session != null) return true;
		else return false;
	}*/
	
	private boolean checkPermission(String sessionId) {
		Session session = null;
		if (sessionId != null) {
			SessionManager sessionManager = service.getSessionManager();
			session = sessionManager.accessSession(sessionId);
			return session != null;
		} else {
			return false;		
		}
	}
	
	/**
	 * Return a resource UUID.
	 * 
	 * @param params
	 * 			map without any keys
	 * @return 0 - generation success
	 * 			result UUID in String	
	 * @throws ServiceException
	 */
	public Result generateUuid(IConnection con, Map<String, String> params) throws Exception
	{
		try {
	   		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String uuid = UUID.randomUUID().toString();
			return new Result(uuid);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);	
		}
	}
	
	public Result memberRegister(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String email = params.get("email");
    		String password = params.get("password"); 
    		String nickname = params.get("nickname");
    		return service.memberRegister(email,password,nickname);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}	
	}
	
	public Result memberRegisterActivate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {		
			String email = params.get("email");
			String password = params.get("password");
			return service.memberRegisterActivate(email,password);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result memberRegisterInvite(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String email = params.get("email");
    		String memberID = params.get("memberId");
    		return service.memberRegisterInvite(email,memberID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result memberRegisterInviteActivate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String email = params.get("email");
    		String password = params.get("password");
    		return service.memberRegisterInviteActivate(email,password);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result memberLogin(IConnection conn, Map<String, String> params)
	{
		try {
    		String email = params.get("email");
    		String password = params.get("password");
    
    		//boolean result = service.memberLogin(email, password);
    		Result result = service.memberLogin(email, password);
    		if (!result.isSuccess())
    			return result;
    
    		// login successfully, create session
    		SessionManager sessionManager = service.getSessionManager();
    		String sessionId = sessionManager.createSession(email);
    
    		System.out.println("SessionID: "+sessionId);
    		return new Result(sessionId);
    	} catch (Exception e) {
    		e.printStackTrace();
    		return new Result(ErrorType.SYSTEM_ERROR);
    	}
	}

	public Result bookCommentCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
			String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookID = params.get("bookId");
    		String memberID = params.get("memberId");
    		String comment = params.get("comment");
    		return service.bookCommentCreate(bookID,memberID,comment);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result bookCommentDelete(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookCommentID = params.get("bookCommentId");
    		String memberID = params.get("memberId");	
    		return service.bookCommentDelete(bookCommentID,memberID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result bookCoverVersionUpload(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookID = params.get("bookId");
    		String memberID = params.get("memberId");
    		String image = params.get("image");
    		return service.bookCoverVersionUpload(bookID,memberID,image);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result bookCoverFilter(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String uuid = params.get("uuid");
    		String subtype = params.get("subtype");
    		String ratio = params.get("ratio");
    		String x = params.get("x");
    		String y = params.get("y");
    		String width = params.get("width");
    		String height = params.get("height");
    		return service.bookCoverFilter(uuid, subtype, ratio, x, y, width, height);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}	
	
	public Result bookCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookName = params.get("bookName");
    		String author = params.get("author");
    		String memberID = params.get("memberId");
    		return service.bookCreate(bookName,author,memberID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result bookFavoriteCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookID = params.get("bookId");
    		String memberID = params.get("memberId");
    		return service.bookFavoriteCreate(bookID, memberID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result bookFavoriteDelete(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookID = params.get("bookId");
    		String memberID = params.get("memberId");
    		return service.bookFavoriteDelete(bookID, memberID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result countMemberByFavoriteBook(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookID = params.get("bookId");
    		return service.countMemberByFavoriteBook(bookID);		
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}		
	}
		
	public Result bookInfoVersionCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookID = params.get("bookId");
    		String name = params.get("name");
    		String subTitle = params.get("subTitle");
    		String abstract_ = params.get("abstract");
    		String author = params.get("author");
    		String authorIntro = params.get("authorIntro");
    		String publishPress = params.get("publishPress");
    		String publishYear = params.get("publishYear");
    		String chapterCount = params.get("chapterCount");
    		String wordCount = params.get("wordCount");
    		String completeStatus = params.get("completeStatus");
    		String website = params.get("website");
    		String url = params.get("url");
    		String memberID = params.get("memberId");
    		return service.bookInfoVersionCreate(bookID, name, subTitle, abstract_, author,
    				authorIntro, publishPress, publishYear, chapterCount,
    				wordCount, completeStatus, website, url, memberID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result chapterCreate(IConnection conn, Map<String, String> params){
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String bookID = params.get("bookId");
    		String memberID = params.get("memberId");
    		String name = params.get("name");
    		String number = params.get("number");
    		String abstract_ = params.get("abstract");
    		String uuid = params.get("uuid");
    		return service.chapterCreate(bookID,memberID,name,Integer.parseInt(number), abstract_, uuid);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}	
	}
	public Result chapterVersionCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
			String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String chapterID = params.get("chapterId");
    		String memberID = params.get("memberId");
    		String number = params.get("number");
    		String name = params.get("name");
    		String abstract_ = params.get("abstract");
    		String uuid = params.get("uuid");
    		return service.chapterVersionCreate(chapterID,memberID,number,name,abstract_,uuid);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result bookRecommendCreate(IConnection conn, Map<String, String> params) {
    	try {
			String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);		
    		String bookID = params.get("bookId");
    		String memberID = params.get("memberId");
    		String target = params.get("target");
    		String reason = params.get("reason");
    		return service.bookRecommendCreate(bookID, memberID, target, reason);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result bookExpectCreate(IConnection conn, Map<String, String> params) {
		try {
			String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);		
    		String bookID = params.get("bookId");
    		String memberID = params.get("memberId");
    		String reason = params.get("reason");
    		return service.bookExpectCreate(bookID, memberID, reason);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result errorReportCreate(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);		
    		String targetID = params.get("targetId");
    		String memberID = params.get("memberId");
    		String subject = params.get("subject");
    		String body = params.get("body");
    		return service.errorReportCreate(memberID, targetID, subject, body);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
	}
		}
	
	public Result friendDelete(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
			String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberIDFrom = params.get("memberIdFrom");
    		String memberIDTo = params.get("memberIdTo");
    		return service.friendDelete(memberIDTo,memberIDFrom);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result friendInvite(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberIDFrom = params.get("memberIdFrom");
    		String memberIDTo = params.get("memberIdTo");
    		return service.friendInvite(memberIDTo,memberIDFrom);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result friendInviteByEmail(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberIDFrom = params.get("memberIdFrom");
    		String memberToEmail = params.get("memberToEmail");
    		return service.friendInviteByEmail(memberToEmail,memberIDFrom);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}	
	
	public Result friendInviteConfirm(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String friendConfirmID = params.get("friendConfirmId");
    		String memberIDTo = params.get("memberIdTo");
    		return service.friendInviteConfirm(memberIDTo,friendConfirmID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result friendInviteDelete(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String friendConfirmID = params.get("friendConfirmId");
    		String memberIDTo = params.get("memberIdTo");
    		return service.friendInviteDelete(memberIDTo,friendConfirmID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupAdminDemote(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		return service.groupAdminDemote(memberID,groupID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupAdminPromote(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		return service.groupAdminPromote(memberID,groupID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String name = params.get("name");
    		String intro = params.get("intro");
    		String image = params.get("image");
    		String tag = params.get("tag");
    		String publicFlag = params.get("publicFlag");
    		return service.groupCreate(memberID, name, intro, image, tag, publicFlag);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupImageUpload(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		String image = params.get("image");
    		return service.groupImageUpload(memberID, groupID, image);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
    }
	public Result groupInfoUpdate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		String name = params.get("name");
    		String intro = params.get("intro");
    		String tag = params.get("tag");
    		return service.groupInfoUpdate(memberID, groupID, name, intro, tag);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupInvite(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberIDTo = params.get("memberIdTo");
    		String memberIDFrom = params.get("memberIdFrom");
    		String groupID = params.get("groupId");
    		return service.groupInvite(memberIDTo,memberIDFrom,groupID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}   		
	}
	public Result groupInviteConfirm(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberIDTo = params.get("memberIdTo");
    		String groupMemberConfirmID = params.get("groupMemberConfirmId");
    		return service.groupInviteConfirm(memberIDTo,groupMemberConfirmID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupInviteDelete(IConnection conn, Map<String, String> params)
	{
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberIDTo = params.get("memberIdTo");
    		String groupMemberConfirmID = params.get("groupMemberConfirmId");
    		return service.groupInviteDelete(memberIDTo,groupMemberConfirmID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupJoin(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		return service.groupJoin(memberID,groupID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupPrivacyUpdate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		String publicFlag = params.get("publicFlag");
    		return service.groupPrivacyUpdate(memberID, groupID, publicFlag);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupTopicCommentCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupTopicID = params.get("groupTopicId");
    		String comment = params.get("comment");
    		return service.groupTopicCommentCreate(memberID, groupTopicID, comment);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupTopicCommentDelete(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupTopicCommentID = params.get("groupTopicCommentId");
    		return service.groupTopicCommentDelete(memberID, groupTopicCommentID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupTopicCreate(IConnection conn, Map<String, String> params)
	{
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		String title = params.get("title");
    		String body = params.get("body");
    		return service.groupTopicCreate(memberID,groupID,title,body);
		} catch (Exception e) {
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupTopicDelete(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupTopicID = params.get("groupTopicId");
    		return service.groupTopicDelete(memberID,groupTopicID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupTopicSticky(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupTopicID = params.get("groupTopicId");
    		return service.groupTopicSticky(memberID,groupTopicID);
		} catch (Exception e) {
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupTopicUnsticky(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupTopicID = params.get("groupTopicId");
    		return service.groupTopicUnsticky(memberID,groupTopicID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result groupTopicUpdate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String groupTopicID = params.get("groupTopicId");
    		String title = params.get("title");
    		String body = params.get("body");
    		return service.groupTopicUpdate(memberID,groupTopicID,title,body);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	public Result isEmailUnique(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String email = params.get("email");
    		return service.isEmailUnique(email);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    	
	}
	public Result isPasswordCorrect(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String memberID = params.get("memberId");
    		String password = params.get("password");
    		return service.isPasswordCorrect(memberID,password);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}		
	}
	public Result memberInfoUpdate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String nickname = params.get("nickname");
    		String aboutMe = params.get("aboutMe");
    		String birthday = params.get("birthday");
    		String gender = params.get("gender");
    		String locationCity = params.get("locationCity");
    		String locationProvince = params.get("locationProvince");
    		String locationCountry = params.get("locationCountry");
    		String homepage = params.get("homepage");
    		return  service.memberInfoUpdate(memberID, nickname, aboutMe, birthday,
    				gender.charAt(0), locationCity, locationProvince, locationCountry, homepage);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	public Result memberLogout(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String email = params.get("email");
    		String password = params.get("password");
    		return service.memberLogout(email,password);
		} catch (Exception e) {
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	public Result memberPasswordUpdate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String password = params.get("password");
    		return service.memberPasswordUpdate(memberID,password);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	
	public Result memberPasswordRetrieve(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String email = params.get("email");
    		return service.memberPasswordRetrieve(email);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	
	public Result memberPortraitUpload(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String image = params.get("image");
    		String imageBig = params.get("imageBig");
    		String imageSmall = params.get("imageSmall");
    		String imageSquare = params.get("imageSquare");
    		return service.memberPortraitUpload(memberID, image,
    			 imageBig, imageSmall, imageSquare);
    	} catch (Exception e) {
    		return new Result(ErrorType.SYSTEM_ERROR);
    	}    		
	}
	public Result messageDelete(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String messageIDArray = params.get("messageIdArray");
    		return service.messageDelete(memberID,messageIDArray.split(separator));
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	public Result messageSend(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberIDFrom = params.get("memberIdFrom");
    		String memberIDTo = params.get("memberIdTo");
    		String title = params.get("title");
    		String body = params.get("body");
    		return service.messageSend(memberIDFrom,memberIDTo,title,body);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	
	public Result recordAlbumCreate(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String bookID = params.get("bookId");
    		String publicFlag = params.get("publicFlag");
    		String collabFlag = params.get("collabFlag");
    		String name = params.get("name");
    		String publicGroupID = params.get("publicGroupId");
    		return service.recordAlbumCreate(memberID, bookID, publicFlag, publicGroupID, collabFlag, name);
		} catch (Exception e) {
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	
	public Result recordAlbumDelete(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String recordAlbumID = params.get("recordAlbumID");
    		return service.recordAlbumDelete(memberID, recordAlbumID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result recordTaskCreate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String chapterID = params.get("chapterId");
    		String recordAlbumID = params.get("recordAlbumId");
    		return service.recordTaskCreate(recordAlbumID, chapterID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}

	public Result recordTaskPublish(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String recordTaskID = params.get("recordTaskId");
    		return service.recordTaskPublish(recordTaskID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}

	public Result recordTaskUpdate(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String recordTaskID = params.get("recordTaskId");
    		String status = params.get("status");
    		String latestAudioID = params.get("latestAudioId");
    		return service.recordTaskUpdate(recordTaskID, status, latestAudioID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}

	public Result recordTaskPrepare(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String recordTaskID = params.get("recordTaskId");
    		return service.recordTaskPrepare(recordTaskID);			
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}		
	}
	
	public Result isFriend(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String memberIDTo = params.get("memberIdTo");
    		String memberIDFrom = params.get("memberIdFrom");
    		return service.isFriend(memberIDTo, memberIDFrom);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}

	public Result isMemberOfGroup(IConnection conn, Map<String, String> params) {
		// TODO Auto-generated method stub
		try {
    		String memberID = params.get("memberId");
    		String groupID = params.get("groupId");
    		return service.isMemberOfGroup(memberID, groupID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}    		
	}
	
	public Result audioCreate(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String recordTaskID = params.get("recordTaskId");
    		String syncID = params.get("syncId");
    		String flvAudioID = params.get("flvAudioId");
    		String type = params.get("type");
    		String subtype = params.get("subtype");
    		String name = params.get("name");
    		String uuid = params.get("uuid");
    		String duration = params.get("duration");
    		String memberID = params.get("memberId");
    		String collabMemberIDs = params.get("collabMemberIds");
			return service.audioCreate(recordTaskID, syncID, flvAudioID, type, subtype, name, uuid, duration, memberID, collabMemberIDs);
		 } catch (Exception e) {
			 e.printStackTrace();
			 return new Result(ErrorType.SYSTEM_ERROR);
		 }		
	}
	
	public Result audioUpdate(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String audioID = params.get("audioId");
    		String syncID = params.get("syncId");
    		String name = params.get("name");
    		String duration = params.get("duration");
    		String memberID = params.get("memberId");
    		String collabMemberIDs = params.get("collabMemberIds");
			return service.audioUpdate(audioID, syncID, name, duration, memberID, collabMemberIDs);
		 } catch (Exception e) {
			 e.printStackTrace();
			 return new Result(ErrorType.SYSTEM_ERROR);
		 }		
	}
	
	public Result audioRate(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String audioID = params.get("audioId");
    		String rating = params.get("rating");
			return service.audioRate(audioID, rating);
		 } catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		 }			
	}
	
	public Result audioFilter(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String input = params.get("input");
    		String output = params.get("output");
    		String bitRate = params.get("bitRate");
    		String sampleRate = params.get("sampleRate");
    		String volume = params.get("volume");
    		String startTime = params.get("startTime");
    		String duration = params.get("duration");
    		return service.audioFilter(input, output, bitRate, sampleRate, volume, startTime, duration);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}	
	}
	
	public Result audioFilterProgress(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String input = params.get("input");
    		String output = params.get("output");
    		return service.audioFilterProgress(input, output);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}					
	}
	
	public Result audioDeploy(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String audioUuid = params.get("audioUuid");
    		String deployType = params.get("deployType");
    		String vendor = params.get("vendor");
    		String model = params.get("model");
    		return service.audioDeploy(audioUuid, deployType, vendor, model);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}			
	}
	
	public Result audioDeployProgress(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String deployUuid = params.get("deployUuid");
    		return service.audioDeployProgress(deployUuid);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}
	}
	
	public Result syncCreate(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String uuid = params.get("uuid");
    		String content = params.get("content");
    		return service.syncCreate(uuid, content);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}				
	}
	
	public Result syncUpdate(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String syncId = params.get("syncId");
    		String content = params.get("content");
    		return service.syncUpdate(syncId, content);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}					
	}
	
	public Result deployPlay(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String deployUuid = params.get("deployUuid");
    		String subtype = params.get("subtype");
    		return service.deployPlay(memberID, deployUuid, subtype);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}				
	}
	
	public Result deployDownload(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String deployUuid = params.get("deployUuid");
    		String subtype = params.get("subtype");
    		return service.deployDownload(memberID, deployUuid, subtype);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}		
	}
	
	public Result audioRecord(IConnection conn, Map<String, String> params) {
		try {
    		String sessionId = params.get("sessionId");
    		if(!checkPermission(sessionId))
    			return new Result(ErrorType.INVALID_LOGIN);
    		String memberID = params.get("memberId");
    		String bookID = params.get("bookId");
    		String recordTaskID = params.get("recordTaskId");
    		return service.audioRecord(memberID, bookID, recordTaskID);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(ErrorType.SYSTEM_ERROR);
		}				
	}
	
	
	/*
	 * Test for transaction
	 */
	public Result testTransaction(IConnection conn, Map<String, String> params)
	{
		//service.testTransaction();
		return new Result("test transaction");
	}
}
